package com.ksfzhaohui.nio.channels;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileHole {

	public static void main(String[] args) throws Exception {
		File temp = new File("hole.txt");
		RandomAccessFile file = new RandomAccessFile(temp, "rw");
		FileChannel channel = file.getChannel();
		ByteBuffer byteBuff = ByteBuffer.allocate(100);
		putData(0, byteBuff, channel);
		putData(500000, byteBuff, channel);
		putData(50000, byteBuff, channel);
		
		System.out.println("file size:"+channel.size());
		channel.close();
	}

	private static void putData(int position, ByteBuffer byteBuff,
			FileChannel fileChannel) throws Exception {
		String string = "*<-- location" + position;
		byteBuff.clear();
		byteBuff.put(string.getBytes("US-ASCII"));
		byteBuff.flip();
		fileChannel.position(position);
		fileChannel.write(byteBuff);
	}
}
